// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.gameScene = ck.getGameScene()
    },

    // 只负责角色和砖块的碰撞
    onCollisionEnter: function (playerCollider, self) {

        if (this.gameScene.data.state === ck.GameState.Prepare)
            return

        if (this.gameScene.data.state === ck.GameState.TouchDoor)
            return

        if (this.gameScene.data.state === ck.GameState.Finished)
            return

        if (this.gameScene.data.state === ck.GameState.Dead)
            return

        var player = playerCollider.node.getComponent('Player')

        // 1st step 
        // get pre aabb, go back before collision
        var playerAabb = playerCollider.world.aabb;
        var playerPreAabb = playerCollider.world.preAabb.clone();

        var selfAabb = self.world.aabb;
        var selfPreAabb = self.world.preAabb.clone();


        // selfPreAabb.x = selfAabb.x;
        // playerPreAabb.x = playerAabb.x;


        // selfPreAabb.y = selfAabb.y;
        // playerPreAabb.y = playerAabb.y;

        if (cc.Intersection.rectRect(playerAabb, selfAabb)) {
            // ck.log('碰撞 playerPreAabb', playerPreAabb.yMin)
            // ck.log('碰撞 playerAabb', playerAabb.yMin)
            // ck.log('碰撞 selfPreAabb', selfPreAabb.yMax)
            // ck.log('碰撞 selfAabb', selfAabb.yMax)
            // if (playerPreAabb.yMin>=selfPreAabb.yMax) {
            if (player.getVDirection() === ck.PlayerDirection.Down) {
                ck.log('collision2')

            	player.putColliderAbovePlatformWorldY(selfAabb.yMax)
                this.resetPlayerDropDown = true
            }
        }
    },

    onCollisionExit(playerCollider, self) {
        if (this.resetPlayerDropDown) {
            var player = playerCollider.node.getComponent('Player')
            player.setNeedDropDown(true)
            
            this.resetPlayerDropDown = false
        }

    },
});
